﻿using DongFangGuoKai.WMSPlatform.Service.LogApp.Dto;
using Microsoft.AspNetCore.Mvc;
using System.Text;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers.LogManage
{
    [Route("api/[controller]")]
    [ApiController]
    public class FileLogController : ApiBaseController
    {
        [HttpGet]
        [Route("GetList")]
        public IActionResult GetList()
        {
            List<TextDto> textDtos = new List<TextDto>();
            string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            string logDirectoryName = "Logs";
            string runTimesPath;
            bool isProduction = environment == "Production";
            if (isProduction)
            {
                runTimesPath = AppDomain.CurrentDomain.BaseDirectory;

            }
            else
            {
                runTimesPath = Environment.CurrentDirectory;
            }
            runTimesPath = Path.Combine(runTimesPath, logDirectoryName);
            if (Directory.Exists(runTimesPath))
            {
                var dires = Directory.GetDirectories(runTimesPath);
                if (dires != null && dires.Length > 0)
                {
                    foreach (var directoryPath in dires)
                    {
                        DirectoryInfo directory = null;
                        try
                        {
                            directory = new DirectoryInfo(directoryPath);

                        }
                        catch (DirectoryNotFoundException e)
                        {
                            Console.WriteLine(e.Message);
                            continue;
                        }
                        var text = new TextDto()
                        {
                            DirectoryName = directory.Name,
                            FullName = directory.FullName,
                            TextDetails = new List<TextDetailDto>()
                        };

                        var fileS = Directory.GetFiles(directoryPath, "*.log");
                        if (fileS != null && fileS.Length > 0)
                        {
                            foreach (var file in fileS)
                            {
                                FileInfo fi = null;
                                try
                                {
                                    fi = new FileInfo(file);
                                    text.TextDetails.Add(new TextDetailDto()
                                    {
                                        Name = fi.Name,
                                        Extension = fi.Extension,
                                        FullName = fi.FullName,
                                    });
                                }
                                catch (FileNotFoundException e)
                                {

                                    Console.WriteLine(e.Message);
                                    continue;
                                }

                            }
                        }
                        textDtos.Add(text);

                    }
                }
            }

            return Success(textDtos);

        }

        [HttpGet]
        [Route("CatDetail")]
        public IActionResult CatDetail(string logPath)
        {

            string fileName = logPath; // 文件路径   
            string fileContent;
            if (!System.IO.File.Exists(fileName))
            {
                return Failed("文件路径不正确");
            }
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fileContent = Encoding.Default.GetString(buffer);
                // 使用fileContent进行后续操作
            }
            fileContent = "" + fileContent;
            return Success<string>(fileContent);
        }
    }
}
